package cheme.ProcessEditor.Policies;

import java.util.List;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.editpolicies.ContainerEditPolicy;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.gef.requests.GroupRequest;

import cheme.ProcessModel.IChild;
import cheme.ProcessModel.IParent;
import cheme.ProcessModel.ITranslatable;
import cheme.ProcessModel.Cmds.AddCmd;
import cheme.ProcessModel.Cmds.ChangeLocationCmd;
import cheme.ProcessModel.Cmds.OrphanChildCmd;

public class DescComponentContainerPolicy extends ContainerEditPolicy
{
	@Override
	public Command getCommand(Request request)
	{
		return super.getCommand(request);
	}

	@Override
	protected Command getCreateCommand(CreateRequest request)
	{
		return null;
	}

	@Override
	protected Command getOrphanChildrenCommand(GroupRequest request)
	{
		List editParts = request.getEditParts();
		CompoundCommand result = new CompoundCommand("Group Orphan");
		for(int i = 0; i < editParts.size(); i++)
		{
			OrphanChildCmd orphan = new OrphanChildCmd((IChild)((EditPart)editParts.get(i)).getModel());
			result.add(orphan);
		}
		return result.unwrap();
	}

//	@Override
//	protected Command getAddCommand(GroupRequest request)
//	{
//		System.out.println("DescComponentContainerPolicy.getAddCommand");
//		
//		CompoundCommand result = new CompoundCommand("Group Add + ChangeBounds");
//		
//		IChild childModel = (IChild)childEditPart.getModel();
//		
//		AddCmd add = new AddCmd();
//			add.setParent(parent);
//			add.setChild(childModel);
//			
//			result.add(add);
//
//		ChangeBoundsCmd changeBounds = new ChangeBoundsCmd();
//		Rectangle rect = (Rectangle)constraint;
//		
//			changeBounds.setModel(childModel);
//			changeBounds.setConstraint(rect);
//			
//			result.add(changeBounds);
//		
//		return result.unwrap();
//	}
	
	protected Command createAddCommand(Request request, EditPart childEditPart, Object constraint)
	{
		Command cmd = null;
		
		ITranslatable childModel = (ITranslatable)childEditPart.getModel();
		Rectangle rect = (Rectangle)constraint;
		AddCmd add = new AddCmd(childModel, (IParent)getHost().getModel());
		ChangeLocationCmd changeBounds = new ChangeLocationCmd(childModel, rect.getLocation());
	
		cmd = add.chain(changeBounds);
				
		return cmd;
	}
	
	@Override
	protected Command getAddCommand(GroupRequest request)
	{
//		ChangeBoundsRequest request = (ChangeBoundsRequest)generic;
//		List editParts = request.getEditParts();
//		CompoundCommand command = new CompoundCommand();
//		
//		GraphicalEditPart childPart;
//		Rectangle r;
//		Object constraint;
//
//		for (int i = 0; i < editParts.size(); i++)
//		{
//			childPart = (GraphicalEditPart)editParts.get(i);
//			r = childPart.getFigure().getBounds().getCopy();
//			
//			//convert r to absolute from childpart figure
//			childPart.getFigure().translateToAbsolute(r);
//			r = request.getTransformedRectangle(r);
//			
//			//convert this figure to relative 
//			getLayoutContainer().translateToRelative(r);
//			getLayoutContainer().translateFromParent(r);
//			r.translate(getLayoutOrigin().getNegated());
//			constraint = getConstraintFor(r);
//			command.add(createAddCommand(generic, childPart, translateToModelConstraint(constraint)));
//		}
//		
//		return command.unwrap();
		
		return null;
	}

}
